package xcmg.device.service.task;

import com.google.common.collect.Lists;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Service;
import xcmg.device.dao.entity.PriMaterialsDO;
import xcmg.device.dao.entity.company.CompanyInfoDO;
import xcmg.device.dao.mapper.CompanyInfoMapper;
import xcmg.device.dao.mapper.PriMaterialsMapper;
import xcmg.syn.feign.SynClient;
import xcmg.syn.service.vo.mmpart.GpsPartVO;
import yb.ecp.fast.infra.infra.ActionResult;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
@Slf4j
public class SyncGpsTaskService {

    private static final Integer BATCH_SIZE = 5000;

    @Resource
    private SynClient synClient;
    @Resource
    private PriMaterialsMapper priMaterialsMapper;
    @Resource
    private CompanyInfoMapper companyInfoMapper;


    public void syncGpsPart(Date t) {
        GpsPartVO gpsPartVO = new GpsPartVO();
        CompanyInfoDO G1CompanyInfo = companyInfoMapper.itemByOrgCode("GAC");
        String maxPartNo = null;
        while (true) {
            gpsPartVO.setLastModifiedTimeStart(t);
            gpsPartVO.setLimitNum(BATCH_SIZE);
            gpsPartVO.setMinPartNo(maxPartNo);
            ActionResult<List<GpsPartVO>> listActionResult = synClient.listPart(gpsPartVO);
            if (listActionResult.getCode() != 0) {
                log.error("同步GPS数据失败");
                break;
            }
            if (listActionResult.getValue() == null || listActionResult.getValue().size() == 0) {
                log.info("没有需要同步的GPS数据");
                break;
            }
            List<GpsPartVO> listActionResultValue = listActionResult.getValue();
            boolean fullPage = (listActionResultValue.size() == BATCH_SIZE);
            maxPartNo = listActionResultValue.get(listActionResultValue.size() - 1).getPartNo();
            List<PriMaterialsDO> updateList = new ArrayList<>();
            for (GpsPartVO gpsPartItem : listActionResultValue) {
                String partNo = gpsPartItem.getPartNo();
                String arrFactory = gpsPartItem.getArrFactory();
                if (partNo == null || arrFactory == null) {
                    continue;
                }
                PriMaterialsDO priMaterialsDO = new PriMaterialsDO();
                priMaterialsDO.setMaterialsNo(partNo);
                priMaterialsDO.setOrgId(G1CompanyInfo.getOrgId());
                priMaterialsDO.setMaterialsDes(gpsPartItem.getPartNameZh());
                priMaterialsDO.setArrFactory("GAC");
                priMaterialsDO.setMaterialsDesEn(gpsPartItem.getPartNameEn());
                priMaterialsDO.setMaterialsShortNo(gpsPartItem.getPartShortNo());
                priMaterialsDO.setUnit(gpsPartItem.getUserageAmountUnit());
                priMaterialsDO.setCreateDate(gpsPartItem.getCreationTime());
                priMaterialsDO.setUpdateDate(gpsPartItem.getLastModifiedTime());
                priMaterialsDO.setModelCode(gpsPartItem.getModelCode());
                priMaterialsDO.setPurchaseType(gpsPartItem.getPurchaseType());
                updateList.add(priMaterialsDO);
            }
            if (CollectionUtils.isNotEmpty(updateList)) {
                Lists.partition(updateList, 50).forEach(priMaterialsDOList -> {
                    priMaterialsMapper.batchSaveGps(priMaterialsDOList);
                });
            }
            if (!fullPage) {
                log.info("当前查询不满页,同步GPS数据完成,size:{},maxPartNo:{}", listActionResultValue.size(), maxPartNo);
                break;
            } else {
                log.info("当前查询满页,maxPartNo:{}", maxPartNo);
            }
        }
    }
}
